# -*- coding:utf-8 -*-
import json
import os
import sys
import pytest
import requests
from core import DataBaseMysql
from core import DataProcess
from core import ReadFileClass
from core import ReportStyle
from core import HttpRequest
from core import Jwt

rfc = ReadFileClass("config.yaml")
base_dir = os.path.dirname(os.path.abspath(__file__))
lib_dir = rfc.get_config("virtual_settings.lib_dir").current
sys.path.append(base_dir)
sys.path.append(lib_dir)


@pytest.fixture(scope="session")
def data_clearing():
    """数据清理"""
    from core import DataClear

    with DataClear(rfc) as dc:
        dc.backup()
        yield
        dc.recovery()


@pytest.fixture(scope="session")
def get_db():
    """数据库对象"""
    with DataBaseMysql(rfc) as db:
        yield db


@pytest.fixture(scope="session")
def clear_db(data_clearing):
    """同时使用数据清洗 和 数据库操作功能"""
    with DataBaseMysql(rfc) as db:
        yield db


@pytest.fixture(scope="session")
def get_token():
    print("开始自动生成token")
    client_url = ReadFileClass("config.yaml").get_config("$.test_basic_token.current_basic_token.client_url").current
    basic_auth = ReadFileClass("config.yaml").get_config("$.test_basic_token.current_basic_token.basic_token").current
    headers = {
        "Authorization": basic_auth
    }
    url = client_url + "oauth/token"
    # "https://email-qa.talkdeskqaid.com/oauth/token"
    data = {
        "grant_type": "client_credentials",
        # "grant_type": "refresh_token",
        "usr": "jun.han@china.talkdesk.com",
        "uid": "624eac3ec8e99045f13c2c8e",

    }
    resp = requests.request(url=url, method="post", data=data, headers=headers)
    # print("resp is ",resp.text)
    resp_dict = json.loads(resp.text)
    token_type = resp_dict["token_type"]
    access_token = resp_dict["access_token"]
    bearer_token = token_type + " " + access_token
    return bearer_token
    # ReadFileClass.remove_py_files(rfc.get_config("$.file_path.case_folder").current)

@pytest.fixture(scope="session")
def refresh_token():
    header = {
        "kid": "53bccad893e349ce93e497fc9e3f351",
        "alg": "ES256"
    }
    payload = {
        "iss": "https://www.talkdeskqaid.com",
        "aud": "https://api.talkdeskqa.com",
        "exp": 1650388326,
        "nbf": 1650387726,
        "iat": 1650387726,
        "jti": "8885b227fcbb4293abf5ef4cf49f25e8",
        "cid": "4af2f201e73e4766bd9dfa6431810790",
        "gty": "refresh_token",
        "scp": [
            "account-phones:read",
            "account:read",
            "acls:read",
            "assets:download",
            "assets:read",
            "assets:write",
            "callbar",
            "case-actions:read",
            "case-automation-groups:read",
            "case-automation-groups:write",
            "case-automations:read",
            "case-automations:write",
            "case-conditions:read",
            "case-dicts:read",
            "case-events-pool:read",
            "case-fields:read",
            "case-fields:write",
            "case-forms:read",
            "case-forms:write",
            "case-notes:read",
            "case-notes:write",
            "case-sla-policies:read",
            "case-sla-policies:write",
            "case-sla-targets:read",
            "case-sla-targets:write",
            "case-tags-manage:read",
            "case-tags:read",
            "case-tenant-apps:read",
            "case-tenant-permission:write",
            "case-views:read",
            "case-views:write",
            "cases:read",
            "cases:write",
            "contacts:read",
            "cti:messaging",
            "customer-data-store:read",
            "email-touchpoints:read",
            "graph-users:read",
            "groups:read",
            "interaction-contacts:read",
            "notifications:read",
            "omnichannel-inbox:write",
            "openid",
            "policies:evaluate",
            "proxy:read",
            "recordings:read",
            "tenant-config:read",
            "voicemails:read"
        ],
        "rlm": "MAIN",
        "aid": "609a416bfc6bb0e4b57459b6",
        "acc": "email-qa",
        "bbb": "11111",
        "psn": "INTERNAL",
        "sub": "1f3361bb7d234b02ad9eec9391e3161b",
        "usr": "jun.han@china.talkdesk.com",
        "uid": "624eac3ec8e99045f13c2c8e",
        "sid": "cd438f158526486699ebc3dc069c641e"
    }
    new_token = Jwt.encode(payload, "1234567", 300)
    return "Bearer "+new_token.decode("utf-8")